package com.ishitong.platform;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;

/**
 * 系统模块动态加载器
 * 
 * @author Administrator
 * 
 */
public class ODApplicationConfigLoad
{
	private Log logger = LogFactory.getLog(this.getClass());
	/**
	 * 模块加载配置信息
	 */
	private static Map<String, ODApplicationConfig> modulesConfig = new HashMap<String, ODApplicationConfig>();

	public ODApplicationConfigLoad()
	{
	}

	/**
	 * 加载模块配置信息
	 */
	public void loadApplication()
	{
		logger.info("loading application module config file...");
		List<File> moduleFile = this.getApplicationFile();
		SAXBuilder buder = new SAXBuilder();
		for (File file : moduleFile)
		{
			try
			{
				logger.info("loading application file " + file.getPath() + "....");
				Document document = buder.build(file);
				Element root = document.getRootElement();
				List moduleNode = XPath.selectNodes(root, "/application");
				if (moduleNode.size() > 0)
				{
					for (int i = 0; i < moduleNode.size(); i++)
					{
						ODApplicationConfig module = new ODApplicationConfig();
						{
							// 取得模块名称,标题
							Element moduleElement = (Element) moduleNode.get(i);
							String name = moduleElement.getAttributeValue("name");
							module.setName(name);
							String title = moduleElement.getAttributeValue("title");
							module.setTitle(title);
							String deploy = moduleElement.getAttributeValue("deploy", "true");
							module.setDeploy(Boolean.parseBoolean(deploy));
							// 取得所有子节点
							List childNode = moduleElement.getChildren();
							for (int j = 0; j < childNode.size(); j++)
							{
								Element childElement = (Element) childNode.get(j);
								String nodeName = childElement.getName();
								if (nodeName.toLowerCase().equals("descript"))
								{
									String descript = childElement.getTextTrim();
									module.setDescript(descript);
								} else if (nodeName.toLowerCase().equals("struts"))
								{

									List valueNode = childElement.getChildren("value");
									List<String> strutsConfig = new ArrayList<String>();
									for (int v = 0; v < valueNode.size(); v++)
									{
										Element valueElement = (Element) valueNode.get(v);
										strutsConfig.add(valueElement.getTextTrim());
									}
									module.setStrutsConfig(strutsConfig);

								} else if (nodeName.toLowerCase().equals("spring"))
								{

									List valueNode = childElement.getChildren("value");
									List<String> springConfig = new ArrayList<String>();
									for (int v = 0; v < valueNode.size(); v++)
									{
										Element valueElement = (Element) valueNode.get(v);
										springConfig.add(valueElement.getTextTrim());
									}
									module.setSpringConfig(springConfig);

								} else if (nodeName.toLowerCase().equals("hibernate"))
								{

									List valueNode = childElement.getChildren("value");
									List<String> hibernateConfig = new ArrayList<String>();
									for (int v = 0; v < valueNode.size(); v++)
									{
										Element valueElement = (Element) valueNode.get(v);
										hibernateConfig.add(valueElement.getTextTrim());
									}
									module.setHibernateConfig(hibernateConfig);

								} else if (nodeName.toLowerCase().equals("freesql"))
								{
									List valueNode = childElement.getChildren("value");
									List<String> freesqlConfig = new ArrayList<String>();
									for (int v = 0; v < valueNode.size(); v++)
									{
										Element valueElement = (Element) valueNode.get(v);
										freesqlConfig.add(valueElement.getTextTrim());
									}
									module.setFreesqlConfig(freesqlConfig);

								} else if (nodeName.toLowerCase().equals("init"))
								{
									List valueNode = childElement.getChildren("bean");
									List<String> beanConfig = new ArrayList<String>();
									for (int v = 0; v < valueNode.size(); v++)
									{
										Element valueElement = (Element) valueNode.get(v);
										if (valueElement.getTextTrim() != null && (!valueElement.getTextTrim().equals("")))
										{
											beanConfig.add(valueElement.getTextTrim());
										}
									}
									module.setInitClass(beanConfig);
								}
							}
							if (module.isDeploy())
							{
								modulesConfig.put(file.getPath(), module);
							}
						}
					}
				}

			} catch (Exception e)
			{
				logger.error(e);
			}
		}

	}

	public static Map<String, ODApplicationConfig> getApplicationConfig()
	{
		return ODApplicationConfigLoad.modulesConfig;
	}

	/**
	 * 取得所有的模块配置文件
	 * 
	 * @return
	 */
	private List<File> getApplicationFile()
	{
		String path = getClass().getResource("").getPath();
		path = path.substring(0, path.indexOf("WEB-INF") + "WEB-INF".length()) + "/applications/";
		path = path.replaceAll("%20", " ");
		List<File> moduleFile = new ArrayList<File>();
		File file = new File(path);
		File[] tfileList = file.listFiles();
		if (file.isDirectory())
		{
			File[] fileList = file.listFiles();
			for (int i = 0; i < fileList.length; i++)
			{
				String fileName = fileList[i].getName();
				if (fileName.endsWith(".app.xml"))
				{
					moduleFile.add(fileList[i]);
				}
			}
		}
		return moduleFile;
	}
}
